#### Tables S19 and S20: OLS predictors for norms/violence ####

# Libraries
# library(here)
# library(rio)
# library(tidyverse)
# library(stargazer)

# data_pnas = import(here("Data","data_pnas.rds"))

gov_parties = import(here("Data","gov_parties.csv")) |> 
  mutate(across(ends_with("_date"), dmy)) 

# Basic Recoding of DVs
data_dv = data_pnas |> 
  filter(pid != "Independent") |> 
  left_join(gov_parties, join_by(state == state,
                                 between(date, inaug_date, end_date))) |> 
  ## Independent Variables
  # Demographics
  mutate(
    white = case_when(
      race == 1 ~ "White",
      race %in% 2:9 ~ "Non-white",
      T ~ NA_character_),
    college = case_when(
      educ %in% 1:4 ~ "No College",
      educ %in% 5:6 ~ "College"),
    urban = case_match(
      urbanicity2,
      1 ~ "Big city",
      2 ~ "Smaller city",
      3 ~ "Suburban",
      4 ~ "Small town",
      5 ~ "Rural",
      .default = NA),
    male = ifelse(gender == 1, 1, 0),
    age = year(date) - birthyr,
    faminc = ifelse(faminc_new == 97, NA, faminc_new),
    bornagain = ifelse(bornagain == "Born again", 1, 0)
  ) |> 
  # Attitudes
  mutate(
    strong = case_when(
      pid7 %in% c(1,7) ~ 1,
      pid7 %in% 2:6 ~ 0
    ),
    poli_interest = case_when(
      newsint %in% 1:4 ~ abs(newsint-4),
      T ~ NA_integer_
    ),
    oppo_gov = case_when(
      (pid != gov_party) & (pid %in% c("Democrat","Republican")) ~ 1,
      pid == "Independent" ~ 0,
      pid == gov_party ~ -1,
      T ~ NA_integer_
    )
  ) |> 
  select(pid, white, college, bornagain, urban, male, age, faminc,
         strong, poli_interest, outparty, inparty,
         oppo_gov, violence1re:violence6re, norm_judgesre:norm_loyaltyre, weight) |> 
  drop_na()

dv_vars = c("norm_judgesre","norm_loyaltyre","norm_pollingre",
            "norm_executivere","norm_censorshipre", paste0("violence",3:6,"re"))

ols_out = map(dv_vars, \(x){
  
  f = as.formula(paste0(x, " ~ pid + white + college + bornagain + urban + male + age + faminc + 
                        strong + poli_interest + oppo_gov + inparty + outparty"))
  #svyglm(f, design = as_survey_design(data_dv, weights = weight))
  lm(f, data = data_dv)
})

# Norms
stargazer(ols_out[1:5],
          title = "OLS Predictors of Democratic Norm Violations (Unweighted)",
          dep.var.labels = c("Ignore Courts", "Loyalty", "Polling Stations",
                             "Exec. Power", "Censorship"),
          covariate.labels = c("Republican",
                               "White",
                               "College-Educated",
                               "Born Again",
                               "Urban/Rural - Rural",
                               "Urban/Rural - Small town",
                               "Urban/Rural - Smaller city",
                               "Urban/Rural - Suburban",
                               "Male",
                               "Age",
                               "Family Income",
                               "Strong Partisan",
                               "Interest in Politics",
                               "Opposition Governor",
                               "In-Party Affect",
                               "Out-Party Affect"),
          notes = "Estimated with two-sided tests",
          star.cutoffs = c(0.05, 0.01, 0.001),
          omit.stat = c("aic","bic"),
          type = "latex",
          out = here("Tables","Supplementary","table_s19.tex"),
          header = F)

# Violence
stargazer(ols_out[6:9],
          title = "OLS Predictors of Political Violence (Unweighted)",
          dep.var.labels = c("Assault","Arson", "Deadly Weapon", "Murder"),
          covariate.labels = c("Republican",
                               "White",
                               "College-Educated",
                               "Born Again",
                               "Urban/Rural - Rural",
                               "Urban/Rural - Small town",
                               "Urban/Rural - Smaller city",
                               "Urban/Rural - Suburban",
                               "Male",
                               "Age",
                               "Family Income",
                               "Strong Partisan",
                               "Interest in Politics",
                               "Opposition Governor",
                               "In-Party Affect",
                               "Out-Party Affect"),
          notes = "Estimated with two-sided tests",
          star.cutoffs = c(0.05, 0.01, 0.001),
          omit.stat = c("aic","bic"),
          type = "latex",
          out = here("Tables","Supplementary","table_s20.tex"),
          header = F)